import 'package:flutter/material.dart';
import 'package:star_author/config/base_configuration.dart';

class BaseButtonWidget extends StatelessWidget {
  final bool enabled;
  final LinearGradient? gradient;
  final Color? backgroundColor;
  final Color? splashColor;
  final Color? highlightColor;
  final Widget child;
  final Border? border;
  final BorderRadius? radius;
  final Function()? onPressed;
  final Function()? onLongPressed;

  /// 可生成渐变底色(值为[LinearGradient])与带圆角的按钮
  BaseButtonWidget({
    Key? key,
    required this.enabled,
    this.gradient,
    this.backgroundColor: const Color(0xffffffff),
    this.splashColor,
    this.highlightColor,
    required this.child,
    this.border,
    this.radius,
    this.onPressed,
    this.onLongPressed,
  }) : super(key: key);

  Color getSplashColor() {
    if (this.enabled) {
      if (this.splashColor == null) {
        return ColorConf.white.withOpacity(0.4);
      } else {
        return this.splashColor!;
      }
    } else {
      return ColorConf.transparent;
    }
  }

  Color getHighlightColor() {
    if (this.enabled) {
      if (this.highlightColor == null) {
        return ColorConf.white.withOpacity(0.2);
      } else {
        return this.highlightColor!;
      }
    } else {
      return ColorConf.transparent;
    }
  }

  @override
  Widget build(BuildContext ctx) {
    return InkWell(
      child: Container(
        clipBehavior: Clip.hardEdge,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          border: this.border,
          borderRadius: this.radius ?? BorderRadius.circular(50.0),
          color: this.backgroundColor!.withOpacity(this.enabled ? 1.0 : 0.4),
          gradient: this.gradient,
        ),
        child: this.child,
      ),
      onTap: this.onPressed,
      onLongPress: this.onLongPressed,
      splashColor: getSplashColor(),
      highlightColor: getHighlightColor(),
      customBorder: RoundedRectangleBorder(
        borderRadius: this.radius ?? BorderRadius.circular(50.0),
      ),
    );
  }
}
